home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 178 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.0 KB  |  80 lines

  1. Path: solon.com!not-for-mail
  2. From: tanmoy@qcd.Lanl.GOV (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c.moderated,comp.std.c
  4. Subject: Re: Function returning structure
  5. Date: 24 Jan 1996 11:07:12 -0600
  6. Organization: Los Alamos National Laboratory
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4e5p00$6g9@solutions.solon.com>
  10. References: <4e2ki8$l0k@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13. --text follows this line--
  14.  
  15. Note cross-post to comp.std.c
  16.  
  17. In article <4e2ki8$l0k@solutions.solon.com> bnelson@netcom.com (Bob
  18. Nelson) writes: 
  19. <snip>
  20.    /*
  21.    Consider the following code -- in which a structure member is of type
  22.    array of char and an instance of that structure is returned from a
  23.    a function.
  24.  
  25.    The Standard [6.3.2.3] says that the dot operator designates a member
  26.    of a structure. (It goes on to add -- in its example text -- that f().x
  27.    is a valid postfix expression though not an lvalue, presuming that
  28.    f is a function returning a structure and that x is member).
  29.  
  30.    With that in mind -- does the standard have anything that might
  31.    disqualify member selection (in a non-assignment context) if that
  32.    member is of type array and that structure is returned by a function?
  33.  
  34. There is nothing wrong selecting that element. However, this element
  35. does not decay to a pointer to its first element. As a result, very
  36. few things can be done to it, except casting it to type void. In
  37. particular, one cannot index it with the [], assign it to a pointer
  38. variable, or pass it to printf or scanf.
  39.  
  40.    One current, popular compiler fails to compile this code -- diagnosing
  41.    an "invalid use of non-lvalue array". 
  42.    */
  43. <snip>
  44.        printf("%s\n", func().c);    
  45.  
  46. begin comp.std.c question {
  47.   Is the following strictly conformant?
  48.  
  49.   #include <stdarg.h>
  50.   #include <stdio.h>
  51.   typedef struct a {char b[5];} a;
  52.   typedef char c[5];
  53.   int f(char *fmt, ...) { char *s;
  54.                           va_list ap; va_start(ap,x); 
  55.                           s = va_arg(ap, c); va_end(ap);
  56.                           return printf(fmt,s); }
  57.   int F(char *fmt, ...) { return 0; }
  58.   a g(void) { static a y; return y; }
  59.   int main(void) { f ( "%s", g().b ); return 0; }
  60.  
  61.   i.e. can non-lvalue arrays be passed to a function and accessed by
  62.   the stdarg macros there? I do not think so, because the `return value'
  63.   of va_arg is not an lvalue, and hence does not decay to a pointer if
  64.   it has an array type.
  65.  
  66.   But what if one replaces f by F in main? i.e. can a non-lvalue array
  67.   be passed to a vararg function at all? There seems to be no
  68.   prohibition against that.
  69. }
  70.  
  71. Cheers
  72. Tanmoy
  73. --
  74. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  75. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  76. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  77. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  78. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  79. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  80.